import 'package:flutter/material.dart';
import '../presentation/screens/home_screen.dart';
import '../presentation/screens/game_screen.dart';
import 'constants.dart';

/// 应用路由配置
/// 
/// 开发思路：
/// 1. 集中式路由管理，便于维护和扩展
/// 2. 类型安全的路由参数传递
/// 3. 支持路由守卫和中间件（V2.0预留）


/// 应用路由配置
class Routes {
  Routes._();
  
  // =================== 路由常量 ===================
  static const String home = '/';
  static const String game = '/game';
  
  // =================== 路由映射 ===================
  static Map<String, WidgetBuilder> get routes {
    return {
      home: (context) => const HomeScreen(),
      // 不要在这里直接映射game路由，因为需要参数
    };
  }
  
  // =================== 路由生成器 ===================
  static Route<dynamic>? onGenerateRoute(RouteSettings settings) {
    debugPrint('导航到: ${settings.name}');
    debugPrint('参数: ${settings.arguments}');
    
    switch (settings.name) {
      case home:
        return MaterialPageRoute(
          builder: (context) => const HomeScreen(),
          settings: settings,
        );
        
      case game:
        final args = settings.arguments;
        if (args is GameScreenArguments) {
          debugPrint('解析参数成功: imagePath=${args.imagePath}, difficulty=${args.difficulty}');
          return MaterialPageRoute(
            builder: (context) => GameScreen(
              imagePath: args.imagePath,
              difficulty: args.difficulty,
            ),
            settings: settings,
          );
        } else {
          debugPrint('参数解析失败，arguments类型: ${args.runtimeType}');
          return MaterialPageRoute(
            builder: (context) => const GameScreen(), // 无参数版本
            settings: settings,
          );
        }
        
      default:
        debugPrint('未找到路由: ${settings.name}');
        return null;
    }
  }
  
  // =================== 导航辅助方法 ===================
  
  /// 导航到游戏页面
  static Future<void> toGameScreen(
    BuildContext context, {
    required String imagePath,
    required DifficultyLevel difficulty,
  }) {
    debugPrint('准备导航到游戏页面');
    debugPrint('imagePath: $imagePath');
    debugPrint('difficulty: $difficulty');
    
    final args = GameScreenArguments(
      imagePath: imagePath,
      difficulty: difficulty,
    );
    
    debugPrint('创建参数对象: $args');
    
    return Navigator.pushNamed(
      context,
      game,
      arguments: args,
    );
  }
  
  /// 返回主页
  static void toHome(BuildContext context) {
    Navigator.pushNamedAndRemoveUntil(
      context,
      home,
      (route) => false,
    );
  }
}

/// 游戏页面参数
class GameScreenArguments {
  final String imagePath;
  final DifficultyLevel difficulty;
  
  const GameScreenArguments({
    required this.imagePath,
    required this.difficulty,
  });
  
  @override
  String toString() {
    return 'GameScreenArguments(imagePath: $imagePath, difficulty: $difficulty)';
  }
}
